package com.gxar.quick.ar.server.base.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gxar.quick.ar.server.db.service.UserService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/11 10:16
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UserMemberExpiredHandler {

    private final UserService userService;

    @XxlJob("userMemberExpiredHandler")
    public void executeUserMemberExpiredHandlerHandler() {
        userService.userMemberExpired();
        log.info("XXL-JOB, user member expired handler");
        XxlJobHelper.log("user member expired execute success");
    }

    @XxlJob("materialExpiredHandler")
    public void executeMaterialExpiredHandlerHandler() {
        String jobParam = XxlJobHelper.getJobParam();
        DateTime yesterday = DateUtil.yesterday();
        if (StringUtils.isNotBlank(jobParam)) {
            yesterday = DateUtil.parse(jobParam, "yyyy-MM-dd");
        }
        DateTime startTime = DateUtil.beginOfDay(yesterday);
        DateTime endTime = DateUtil.beginOfDay(new Date());
        userService.materialExpired(startTime, endTime);
        log.info("XXL-JOB, material expired handler");
        XxlJobHelper.log("material expired execute success");
    }

}
